package personnal.jonat;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

public class ExcelUtils {

    private static final Logger logger = Logger.getLogger(ExcelUtils.class);

    public static HSSFCell getOrCreateCell(HSSFSheet sheet, int rowIndex, int columnIndex) {
        HSSFRow row = sheet.getRow(rowIndex);
        HSSFCell cell = row.getCell(columnIndex);
        if (cell == null) {
            cell = row.createCell(columnIndex);
        }
        return cell;
    }

    public static void setCellStringValue(HSSFCell nameCell, String value) {
        nameCell.setCellType(HSSFCell.CELL_TYPE_STRING);
        nameCell.setCellValue(new HSSFRichTextString(value));
    }

    public static void setCellNumericValue(HSSFCell nameCell, Double value) {
        nameCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        if (value == null) {
            nameCell.setCellValue(0.0);
        } else {
            nameCell.setCellValue(value);
        }
    }

    public static Double getCellValueAsDouble(HSSFCell gradeCell) {
        if (gradeCell == null) {
            logger.warn("Aucune valeur dans la cellule ==> valeur 0");
            return 0.0;
        }
        Double value = null;
        switch (gradeCell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                value = Double.parseDouble(gradeCell.getRichStringCellValue().getString());
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                value = gradeCell.getNumericCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                value = 0.0;
                break;
            default:
                value = 0.0;
                logger.warn("Type de cellule non pris en charge ==> valeur 0");
                if (logger.isDebugEnabled()) {
                    logger.debug(" |-> cellType: " + gradeCell.getCellType() + ", " + gradeCell.toString());
                }
                break;
        }

        return value;
    }

    public static String getCellValueAsString(HSSFCell gradeCell) {
        String stringValue = null;
        switch (gradeCell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                stringValue = gradeCell.getRichStringCellValue().getString();
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                stringValue = String.valueOf(gradeCell.getNumericCellValue());
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                stringValue = "";
                break;
            default:
                stringValue = "";
                logger.warn("Type de cellule non pris en charge ==> valeur vide");
                if (logger.isDebugEnabled()) {
                    logger.debug(" |-> cellType: " + gradeCell.getCellType() + ", " + gradeCell.toString());
                }
                break;
        }
        return stringValue;
    }

}
